package com.example.studentmanagersystem;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fs.myview.AccountListView;
import com.fs.po.UserData;
import com.fs.utils.CmdUtils;

public class AdminAccountUpdateActivity extends Activity {
	
	private EditText inputName,inputPassword;
	private Button submit,actionBarBack;
	
	private int _id;
	private String user;
	private String name;
	private String password;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.admin_account_user_update_activity);
		
		this.init();
		
	}
	/**
	 *initialize widget
	 */
 
	private void init() {
		
		this.inputName=(EditText) this.findViewById(R.id.admin_account_update_nameET);
		this.inputPassword=(EditText) this.findViewById(R.id.admin_account_update_passwordET);
		this.submit=(Button) this.findViewById(R.id.admin_account_update_submit);
		this.actionBarBack=(Button) this.findViewById(R.id.update_info_action_backButton);
		
		this.submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				boolean isName=TextUtils.isEmpty(inputName.getText());
				boolean isPsw=TextUtils.isEmpty(inputPassword.getText());
				
				boolean isRight=isName&&isPsw;
				if(!isRight){
					
					showNormalDia();
					
				}else {
					Toast.makeText(AdminAccountUpdateActivity.this, "请正确输入！", 0).show();
				}
				
			}
		});
		
		//back button
		this.actionBarBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				AdminAccountUpdateActivity.this.finish();
				
			}
		});
		
	}


	
	
	/**
	 * Intent,submit
	 */
	private void submitData() {
		
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		this.user=bundle.getString("user_type");
		this._id=bundle.getInt("user_id");
		
		System.out.println("返回值"+user+"ID:"+_id);
			
			CmdUtils cmdUtils=new CmdUtils(this);
			
			String cmd="update "+user+" set name='"+name+"',password='"+password+"' where _id="+_id+"";
			 
			cmdUtils.submitCmd(URL.CMD_URL,"update",cmd);
			

	}
	
	
	/**
	 * get data that will be update
	 */
	private void updateData() {
		
		this.name=this.inputName.getText().toString().trim();
		this.password=this.inputPassword.getText().toString().trim();
	}
	
	
	 /**
	   * Normal Dialog
	   */
	  private void showNormalDia(){  
	        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());  
	        AlertDialog.Builder normalDia=new AlertDialog.Builder(this);  
	        normalDia.setIcon(R.drawable.sure);  
	        normalDia.setTitle("提示");  
	        normalDia.setMessage("确定修改？");  
	          
	        normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	            	
	            	updateData();
					submitData();
					AdminAccountUpdateActivity.this.finish();
	            	 
	            }  
	        });  
	        
	        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	               
	            }  
	        });  
	        normalDia.create().show();  
	    } 
	
}
